import com.google.gson.Gson;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvException;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @Author Yusc
 * @Date 2023/11/13 22:24
 * @Version
 * @Description 基于openScv读取scv文件
 **/
public class ReadSCV {
    public static void main(String[] args) throws IOException, CsvException {
        String fileUrl = "src/main/resources/data/air_data.csv";
        InputStreamReader reader = new InputStreamReader(Files.newInputStream(Paths.get(fileUrl)), StandardCharsets.UTF_8);

        CSVReader csvReader = new CSVReaderBuilder(reader).build();
        Gson gson = new Gson();
//        // 数据读取 ---> 第一行不需要
        String[] str = csvReader.readNext();

        while (true) {
            str = csvReader.readNext();
            if (str == null) {
                break;
            }
            // 将数据写入spark中
            System.out.println(gson.toJson(str));
        }

        csvReader.close();
    }
}
